#include <iterator>
#include <unistd.h>
#include "MarkupSTL.h"
#include "global.h"
#include "cmd.h"
#include "Game.h"
#include "RedisClient.h"
#include "Logic.h"
#include "json.h"
#include "globalExtern.h"
#include "Host.h"
#include "util.h"
#include "NodeServer.h"



#define  MOD_UID (100)

CGameServer::CGameServer():m_nodecount(0), m_nodeindex(-1), m_initflag(false), m_oldestRecordPoint(0),m_oldestYm(0)
{
}

CGameServer::~CGameServer()
{
}

int CGameServer::OnBYTimeOut(int Timerid)
{
	switch (Timerid)
	{
	case TIMERID_MAINTENANCE_PROC:
	{
		procTimerCacheCheck();
		break;
	}
	default:
		break;
	}
	
	return 0;
}

bool CGameServer::InitGame()
{
	return true;
}

int CGameServer::ProcEventPublishService(int ev_id, SpiderEvent* pEvent)
{
	if (!(pEvent && pEvent->evParam.ptr))
		return -1;

	ServiceConf* pserviceConf = (ServiceConf*)pEvent->evParam.ptr;
	if (SERVER_TYPE_GAMERECORD_STORAGE == pserviceConf->server_type)
	{
		ServiceTypeList::const_iterator itor = m_pHost->m_pNode->m_serviceTypeList.find(SERVER_TYPE_GAMERECORD_STORAGE);
		if (m_pHost->m_pNode->m_serviceTypeList.end() != itor && itor->second.size() > 0)
		{
			int index = 0;
			for (ServiceVect::const_iterator itor1 = itor->second.begin(); itor->second.end() != itor1; ++itor1, ++index)
			{
				if (TGlobal::_svid == itor1->svid)
				{
					break;
				}
			}

			if (index < itor->second.size())
			{
				m_nodecount = itor->second.size();
				m_nodeindex = index;

				log_debug("srv id,%d,index,%d,service num,%d", TGlobal::_svid, index, itor->second.size());
				if (m_nodecount > 0)
				{
				}
				else
				{
					log_error("wrong srv num,srv id,%d,index,%d,service num,%d", TGlobal::_svid, index, itor->second.size());
				}
			}
			else
			{
				log_error("service find no srv id,srv id,%d", TGlobal::_svid);
			}
			log_debug("service pulish ready, srv id,%d", TGlobal::_svid);
		}
		else
		{
			log_error("service pulish not ready");
		}
	}

	return 0;
}

int CGameServer::ProcessPacket(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	unsigned int cmd = inputPkg->GetCmdType();
	switch (cmd)
	{
	default:
		break;
	}

	return 0;
}

int CGameServer::ProcEventGameInit(int ev_id, SpiderEvent* pEvent)
{
	if (!m_initflag)
	{
		StartTimer(TIMERID_MAINTENANCE_PROC, g_maintenace_proc_timer_dur, true);
		m_initflag = true;
	}

	return 0;
}

int CGameServer::procTimerCacheCheck(void)
{
	if (0 == m_nodecount || m_nodeindex < 0)
	{
		log_info("mod not ready,count,%u,index,%d", m_nodecount, m_nodeindex);
		return 0;
	}
	
	time_t tt = ::time(NULL);
	if(m_oldestRecordPoint > 0)
	{
		if (tt <= m_oldestRecordPoint + (86400*g_list_recent_days))
		{
			log_debug("no proc,now,%llu,oldest,%llu",tt, m_oldestRecordPoint);
			return 0;
		}
		
		if (m_oldestYm > 0)
		{
			int ym = 0;
			int dis = 0;
			timestampToym(tt, g_westernmost_timezoneinmin, ym);
			if (monthdis(m_oldestYm, ym, dis))
			{
				if (dis < 1)
				{
					log_debug("no proc,now,%llu,ym,%d,oldestym,%d",tt, ym, m_oldestYm);
					return 0;
				}
			}	
		}

		if (!m_procTable.empty())
		{
			_u64_ seq = 0;
			int nodataflag = 0;

			if (m_dbHandler.getOldestGameTecordItem(m_procTable, seq, nodataflag))
			{
				if (1 == nodataflag)
				{
				}
				

			}
			else
			{
				
			}
		}
		
	}


	return 0;
}

bool CGameServer::monthdis(const int ym1, const int ym2, int& dis)
{
	static unsigned int s_mstrlen = 2;

	if (!(ym1 > 0 && ym2 > 0))
		return false;

	int y1 = ym1/100;
	int m1 = ym1%100;
	int y2 = ym2/100;
	int m2 = ym2%100;
	if (!(y1 > 1900 && y2 > 1900))
		return false;
	if (!(m1 >= 1 && m1 <= 12 && m2 >= 1 && m2 <= 12))
		return false;

	dis = (y2 - y1) * 12 + (m2 - m1);
	return true;
}